package com.ex.mzwqz.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.RadioGroup;

import com.ex.mzwqz.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaguoqiang on 16/10/24.
 */
public class StatisticsActivity extends BaseActivity{
    private LineChart chart;
    private PieChart pieChart;
    private BarChart barChart;
    private RadioGroup rd;

    @Override
    protected int setContentViewResId() {
        return R.layout.activity_statistics;
    }

    @Override
    protected void initView() {
        chart = (LineChart) findViewById(R.id.chart);
        pieChart = (PieChart) findViewById(R.id.pie_chart);
        barChart = (BarChart) findViewById(R.id.bar_chart);
        rd = (RadioGroup) findViewById(R.id.rd_switch);
    }

    @Override
    protected void initData() {
        initLineChart();
        initPieChart();
        initBarChart();
    }

    @Override
    protected void initEvent() {
        rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case 1:
                        chart.setVisibility(View.VISIBLE);
                        pieChart.setVisibility(View.GONE);
                        barChart.setVisibility(View.GONE);
                        break;
                    case 2:
                        chart.setVisibility(View.GONE);
                        pieChart.setVisibility(View.VISIBLE);
                        barChart.setVisibility(View.GONE);
                        break;
                    case 3:
                        barChart.setVisibility(View.VISIBLE);
                        chart.setVisibility(View.GONE);
                        pieChart.setVisibility(View.GONE);
                        break;
                }
            }
        });
    }

    public void initLineChart(){
        List<TestData> dataObjects = new ArrayList<>();
        for(int i = 0 ; i < 10 ; i++)
            dataObjects.add(new TestData(i,i));
        List<Entry> entries = new ArrayList<Entry>();

        for (TestData data : dataObjects) {

            // turn your data into Entry objects
            entries.add(new Entry(data.getValueX(), data.getValueY()));
        }
        LineDataSet dataSet = new LineDataSet(entries, "Line"); // add entries to dataset
        dataSet.setColor(Color.RED);
        dataSet.setValueTextColor(Color.BLACK);
        LineData lineData = new LineData(dataSet);
        chart.setData(lineData);
        chart.invalidate(); // refresh
    }

    public void initPieChart(){
        List<TestData> dataObjects = new ArrayList<>();
        dataObjects.add(new TestData(1,10,"餐饮服务型"));
        dataObjects.add(new TestData(2,20,"生产加工型"));
        dataObjects.add(new TestData(3,30,"销售服务型"));
        List<PieEntry> entries = new ArrayList<>();

        for (TestData data : dataObjects) {

            // turn your data into Entry objects
            entries.add(new PieEntry(data.getValueY(), data.getLab()));
        }
        PieDataSet dataSet = new PieDataSet(entries, "餐饮类型"); // add entries to dataset
        dataSet.addColor(Color.RED);
        dataSet.addColor(Color.BLUE);
        dataSet.addColor(Color.YELLOW);
        dataSet.setValueTextColor(Color.BLACK);
        PieData lineData = new PieData(dataSet);
        pieChart.setData(lineData);
        pieChart.invalidate();
    }

    public void initBarChart(){
        List<TestData> dataObjects = new ArrayList<>();

        dataObjects.add(new TestData(1,10,"昌吉"));
        dataObjects.add(new TestData(2,20,"玛纳斯"));
        dataObjects.add(new TestData(3,30,"富康"));

        List<BarEntry> entries = new ArrayList<>();
        List<BarEntry> entries1 = new ArrayList<>();
        for (TestData data : dataObjects) {
            entries.add(new BarEntry(data.getValueX(), data.getValueY(),data.getLab()));
            entries1.add(new BarEntry(data.getValueX() + 1, data.getValueY() + 1,data.getLab()));
        }
        BarDataSet dataSet = new BarDataSet(entries, "昌吉"); // add entries to dataset
        dataSet.setColor(Color.RED);
        dataSet.setValueTextColor(Color.BLACK);
        BarDataSet dataSet1 = new BarDataSet(entries1, "富康");
        dataSet1.setColor(Color.BLUE);
        dataSet1.setValueTextColor(Color.GRAY);
        BarData lineData = new BarData(dataSet,dataSet1);
        Description description = new Description();
        description.setText("餐饮类型");
        barChart.setDescription(description);
        barChart.setData(lineData);
        barChart.invalidate(); // refresh
    }

    class TestData{
        private float x;
        private float y;
        private String lab;

        public TestData(float x,float y,String lab) {
            this.x = x;
            this.y = y;
            this.lab = lab;
        }

        public TestData(float x,float y) {
            this.x = x;
            this.y = y;
        }

        public void setX(float x) {
            this.x = x;
        }

        public void setY(float y) {
            this.y = y;
        }

        public float getValueX(){
            return x;
        }

        public float getValueY(){
            return y;
        }

        public String getLab() {
            return lab;
        }

        public void setLab(String lab) {
            this.lab = lab;
        }
    }
}
